package com.tuiyun.jxgl.action;

import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Msgtemplate;
import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.bean.Stuselectdriver;
import com.tuiyun.jxgl.dao.DriverHome;
import com.tuiyun.jxgl.dao.MsgtemplateHome;
import com.tuiyun.jxgl.dao.StudentHome;
import com.tuiyun.jxgl.dao.StuselectdriverHome;
import com.tuiyun.jxgl.service.MSGService;

public class SelDriverAction {
	
	private int msg_template_id;

	private Student student;
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}	
	@Transactional
	public String execute(){		
		try{
		Driver d = dh.findById(driverid);
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated()){
			student = (Student) currentUser.getPrincipal();
			Stuselectdriver ssd = ssh.findByStudent(student.getId());
			if(ssd != null){
				return "success";
			}
		}
		Msgtemplate mg = mh.findById(msg_template_id);
		mgs.sendMsg(d, mg, student);
		Stuselectdriver ssd = new Stuselectdriver();
		ssd.setDriver(d);
		ssd.setStuname(student.getRealName());
		ssd.setMsgsend((byte)1);
		ssd.setMsgtemplate(mg);
		ssd.setPickdate(new Date(System.currentTimeMillis()));
		ssd.setStudent(student);
		ssh.attachDirty(ssd);
		
		student.setState(Student.CHOOSED_TA_BUT_NO_PAIED);
		sh.attachDirty(student);
		
		}catch(Exception e){
			e.printStackTrace(System.out);			
		}		
		return "success";
	}
	
	private MSGService mgs;
	
	public int getDriverid() {
		return driverid;
	}
	public void setDriverid(int driverid) {
		this.driverid = driverid;
	}
	public DriverHome getDh() {
		return dh;
	}
	public void setDh(DriverHome dh) {
		this.dh = dh;
	}
	public MsgtemplateHome getMh() {
		return mh;
	}
	public void setMh(MsgtemplateHome mh) {
		this.mh = mh;
	}
	public StudentHome getSh() {
		return sh;
	}
	public void setSh(StudentHome sh) {
		this.sh = sh;
	}
	public StuselectdriverHome getSsh() {
		return ssh;
	}
	public void setSsh(StuselectdriverHome ssh) {
		this.ssh = ssh;
	}
	public MSGService getMgs() {
		return mgs;
	}
	public void setMgs(MSGService mgs) {
		this.mgs = mgs;
	}

	public int getMsg_template_id() {
		return msg_template_id;
	}
	public void setMsg_template_id(int msg_template_id) {
		this.msg_template_id = msg_template_id;
	}	

	private int driverid;
	private DriverHome dh;
	private MsgtemplateHome mh;
	private StudentHome sh;
	private StuselectdriverHome ssh;
	
}
